﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace AcceleratorTests
{
    public class DataHandler
    {
        public List<DataLog> Logs;
        private string del = ";";
        public DataHandler()
        {
            Logs = new List<DataLog>();
        }

        public void saveToFile(string p)
        {
            TextWriter tw = new StreamWriter(p);
            StringBuilder sbheader = new StringBuilder();
            //Write header!
            sbheader.Append("\"function\"");
            sbheader.Append(del);
            sbheader.Append("\"size\"");
            sbheader.Append(del);
            sbheader.Append("\"times tested\"");
            sbheader.Append(del);
            sbheader.Append("\"graph size\"");
            sbheader.Append(del);
            sbheader.Append("\"CPU\"");
            sbheader.Append(del);
            sbheader.Append("\"GPU\"");
            sbheader.Append(del);
            sbheader.Append("\"factor\"");
            sbheader.Append(del);
            sbheader.Append("\"maxdeviation\"");
            sbheader.Append(del);
            sbheader.Append("\"stddeviation\"");
            sbheader.Append(del);
            sbheader.Append("\"average\"");
            tw.WriteLine(sbheader);
            //write the data
            foreach (DataLog dlog in Logs)
            {
                foreach (DataLogEntry entry in dlog.Entries)
                {   
                    StringBuilder sb = new StringBuilder();

                    sb.Append(dlog.name);
                    sb.Append(del);
                    sb.Append(dlog.size );
                    sb.Append(del);
                    sb.Append(entry.timesRun );
                    sb.Append(del);
                    sb.Append(entry.graphSize);
                    sb.Append(del);
                    sb.Append(entry.CPUtime.TotalMilliseconds );
                    sb.Append(del);
                    sb.Append(entry.GPUtime.TotalMilliseconds );
                    sb.Append(del);
                    sb.Append(entry.factor );
                    sb.Append(del);
                    // TODO: Maybe not just select one!!!
                    sb.Append(entry.resultList[0].maxDiff);
                    sb.Append(del);
                    sb.Append(entry.resultList[0].deviation);
                    sb.Append(del);
                    sb.Append(entry.resultList[0].avgDiff);

                    tw.WriteLine(sb.ToString());
                }

            }
            tw.Close();

        }
    }
}
